package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.BizTmp;
import cc.rengu.redp.bizimpl.upmp.service.IBizTmpService;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.BankCodeInfo;
import cc.rengu.redp.bizimpl.upmp.service.BankCodeInfoService;
import cc.rengu.redp.bizimpl.upmp.service.RegionInfoService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Api(tags = "参数管理:联行号管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/admin/merParam/bankCodeInfo")
public class BankCodeInfoController extends BaseController {
    @Autowired
    private BankCodeInfoService bankCodeInfoService;
    @Autowired
    private RegionInfoService regionInfoService;
    @Autowired
    private IBizTmpService bizTmpService;
    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, BankCodeInfo entity) {
        return getDataTable(this.bankCodeInfoService.findByPage(request, entity));
    }

    @ApiOperation("查询单条")
    @GetMapping(value = "/getOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public BankCodeInfo getOne(@NotBlank(message = "{required}") String id) {
        BankCodeInfo entity = bankCodeInfoService.getById(id);
        if (StringUtils.isNotBlank(entity.getRegionCode())) {
            String regionName = regionInfoService.findOneByCode(entity.getRegionCode(), BusinessContainer.data_sta_01) == null ? "" : regionInfoService.findOneByCode(entity.getRegionCode(), BusinessContainer.data_sta_01).getRegionName();
            entity.setRegionName(regionName);
        }
        return entity;
    }

    @ApiOperation("查询审核数据")
    @GetMapping(value = "/getCheckOne")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public BankCodeInfo getCheckOne(@NotBlank(message = "{required}") String id) throws RedpException {
        BankCodeInfo entity = bankCodeInfoService.getById(id);
        //获取临时表数据并转换成实体对象
        BizTmp tmp = bizTmpService.getBizTmpDetail(String.valueOf(id));
        if (tmp==null) {
            log.error("根据id没有找到指定临时表数据！");
            throw new RedpException("根据id没有找到指定临时表数据！");
        }
        String bizdata = "";
        if (null != tmp.getBizData1()) {
            bizdata += tmp.getBizData1();
        }
        if (null != tmp.getBizData2()) {
            bizdata += tmp.getBizData2();
        }
        if (null != tmp.getBizData3()) {
            bizdata += tmp.getBizData3();
        }
        entity = JSON.parseObject(bizdata, BankCodeInfo.class);
        return entity;
    }

    @ApiOperation("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "bankBranchCode", value = "银行网点联行号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bankBranchName", value = "银行网点名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cupsInstId", value = "银联机构号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "bankName", value = "银行名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "regionCode", value = "行政区划代码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "nuccInstId", value = "网联机构号", required = true, dataType = "String")
    })
    public void add(@Valid BankCodeInfo entity) throws RedpException {
        try {
            this.bankCodeInfoService.create(entity);
        } catch (Exception e) {
            BankCodeInfo byBankBranchCode = this.bankCodeInfoService.findByBankBranchCode(entity);
            String message="";
            if (byBankBranchCode!=null){
                 message = "该条数据已存在,不要重复添加";
            }else{
                 message = "新增失败";
            }
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @ApiOperation("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid BankCodeInfo entity) throws RedpException {
        try {
            this.bankCodeInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @ApiOperation("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("areaInfo:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.bankCodeInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
    @ApiOperation("审核")
    @PutMapping("/audit")
//    @RequiresPermissions("cupsInstInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "approvalRefuseReason", value = "审批拒绝原因", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "opt", value = "审核结果", required = true, dataType = "String")
    })
    public void audit(@Valid BankCodeInfo entity, String opt, String approvalRefuseReason) throws RedpException {
        try {
            this.bankCodeInfoService.audit(entity, opt, approvalRefuseReason);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.bankCodeInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除类目失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @ApiOperation("查询所有机构")
    @GetMapping(value = "/bankCodeSelect2List")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "银行网点联行号|银行网点名称", dataType = "String"),
    })

    public  List<BankCodeInfo> bankCodeSelect2List(String q) {
        LambdaQueryWrapper<BankCodeInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(q)) {
            wrapper.and(wp-> wp.likeRight(BankCodeInfo::getBankBranchName, q).
                    or().likeRight(BankCodeInfo::getBankBranchCode,q));
        }
        wrapper.eq(BankCodeInfo::getBankBranchLevel, "00");
        return bankCodeInfoService.list(wrapper);
    }

}
